package com.yumeno.epidemic.service.Impl;

import com.yumeno.epidemic.beans.DailyEpidemicInfo;
import com.yumeno.epidemic.beans.EpidemicDetailInfo;
import com.yumeno.epidemic.beans.EpidemicInfo;
import com.yumeno.epidemic.beans.ProvinceInfo;
import com.yumeno.epidemic.mapper.EpidemicMapper;
import com.yumeno.epidemic.mapper.ProvinceMapper;
import com.yumeno.epidemic.service.EpidemicService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.awt.*;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;

@Service
public class EpidemicServiceImpl implements EpidemicService {

    @Autowired
    private EpidemicMapper epidemicMapper;
    @Autowired
    private ProvinceMapper provinceMapper;

    @Override
    public List<ProvinceInfo> saveData(DailyEpidemicInfo dailyEpidemicInfo, Integer userId) {
        Date current = new Date();
        //拆分数据日期
        String[] ymd = dailyEpidemicInfo.getDate().split("-");
        short year = Short.parseShort(ymd[0]);
        short month = Short.parseShort(ymd[1]);
        short day = Short.parseShort(ymd[2]);
        for (EpidemicInfo epidemicInfo : dailyEpidemicInfo.getDataArray()) {
            //设置录入者编号
            epidemicInfo.setUserId(userId);
            //设置信息录入日期
            epidemicInfo.setInputDate(current);
            //设置数据日期
            epidemicInfo.setDataYear(year);
            epidemicInfo.setDataMonth(month);
            epidemicInfo.setDataDay(day);
            this.epidemicMapper.saveInfo(epidemicInfo);
        }
        return this.provinceMapper.findNoDataProvinces(year, month, day);
    }

    @Override
    public List<EpidemicDetailInfo> findLatestData() {
        Calendar calendar = new GregorianCalendar();
        short year = (short) calendar.get(Calendar.YEAR);
        short month = (short) (calendar.get(Calendar.MONTH)+1);
        short day = (short) calendar.get(Calendar.DATE);
        return this.epidemicMapper.findLatestData(year, month, day);
    }

}
